package com.bonc.modules.rpt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.factory.PageFactory;
import com.bonc.modules.rpt.dao.PeakShavingDao;
import com.bonc.modules.rpt.entity.PeakShavingEntity;
import com.bonc.modules.rpt.param.FixParam;
import com.bonc.modules.rpt.service.FixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jinlong
 * @date 2021/7/30
 */
@Service("peakShavingCity")
@DataSource("data")
public class PeakShavingCityServiceImpl extends ServiceImpl<PeakShavingDao, PeakShavingEntity> implements FixService {

    static String STATIC_REGION_CODE = "00";

    @Autowired
    private PeakShavingDao peakShavingDao;

    @Override
    public String getMaxDate() {
        return peakShavingDao.getMaxDate();
    }

    @Override
    public Page queryPage(FixParam fixParam) {
        LambdaQueryWrapper<PeakShavingEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(fixParam)) {
            if (ObjectUtil.isNotEmpty(fixParam.getDealYear())) {
                queryWrapper.eq(PeakShavingEntity::getDealDate, fixParam.getDealYear());
            }
            if (ObjectUtil.isNotEmpty(fixParam.getCityCode()) && !STATIC_REGION_CODE.equals(fixParam.getCityCode())) {
                queryWrapper.eq(PeakShavingEntity::getRegionCode, fixParam.getCityCode());
            } else {
                queryWrapper.eq(PeakShavingEntity::getAreaCode, STATIC_REGION_CODE);
                queryWrapper.eq(PeakShavingEntity::getGriddingCode, STATIC_REGION_CODE);
            }
            if (ObjectUtil.isNotEmpty(fixParam.getCountyCode()) && !STATIC_REGION_CODE.equals(fixParam.getCountyCode())) {
                queryWrapper.eq(PeakShavingEntity::getAreaCode, fixParam.getCountyCode());
            } else {
                queryWrapper.eq(PeakShavingEntity::getGriddingCode, STATIC_REGION_CODE);
            }
        }
        queryWrapper.eq(PeakShavingEntity::getDimId, "1");
        queryWrapper.orderByAsc(PeakShavingEntity::getRegionCode,PeakShavingEntity :: getAreaCode);
        return this.baseMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
    }

    @Override
    public List queryData(FixParam fixParam) {
        LambdaQueryWrapper<PeakShavingEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(fixParam)) {
            if (ObjectUtil.isNotEmpty(fixParam.getDealYear())) {
                queryWrapper.eq(PeakShavingEntity::getDealDate, fixParam.getDealYear());
            }
            if (ObjectUtil.isNotEmpty(fixParam.getCityCode()) && !STATIC_REGION_CODE.equals(fixParam.getCityCode())) {
                queryWrapper.eq(PeakShavingEntity::getRegionCode, fixParam.getCityCode());
            } else {
                queryWrapper.eq(PeakShavingEntity::getAreaCode, STATIC_REGION_CODE);
                queryWrapper.eq(PeakShavingEntity::getGriddingCode, STATIC_REGION_CODE);
            }
            if (ObjectUtil.isNotEmpty(fixParam.getCountyCode()) && !STATIC_REGION_CODE.equals(fixParam.getCountyCode())) {
                queryWrapper.eq(PeakShavingEntity::getAreaCode, fixParam.getCountyCode());
            } else {
                queryWrapper.eq(PeakShavingEntity::getGriddingCode, STATIC_REGION_CODE);
            }
        }
        queryWrapper.eq(PeakShavingEntity::getDimId, "1");
        queryWrapper.orderByAsc(PeakShavingEntity::getRegionCode);

        return this.baseMapper.selectList(queryWrapper);
    }

}
